from requests.packages.urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
from requests import Session


class XSession(Session):
    """
    enhanced requests session for spiders
    """
    def __init__(self, **kwargs):
        super(XSession, self).__init__()
        retry_num = kwargs.get('max_retries', 1)
        retry_interval = kwargs.get('retry_interval', 0.1)
        retry_status_list = kwargs.get('retry_status_list', [403, 503, 502])
        max_redirects = kwargs.get('max_redirects', 3)
        retries = Retry(
            total=retry_num, backoff_factor=retry_interval,
            status_forcelist=retry_status_list)
        self.mount('http://', HTTPAdapter(max_retries=retries,
                                          pool_connections=1000, pool_maxsize=1000))
        self.mount('https://', HTTPAdapter(max_retries=retries,
                                           pool_connections=1000, pool_maxsize=1000))
        self.max_redirects = max_redirects
